import {  Notify} from 'vant'
import { createRouter, createWebHistory } from 'vue-router'
const Home=()=>import('../views/home/Home')
const Category=()=>import('../views/category/Category')
const Detail=()=>import('../views/detail/Detail')
const Profile=()=>import('../views/profile/Profile')
const Shopcart=()=>import('../views/shopcart/Shopcart')
const Register=()=>import('../views/profile/Register')
const Login=()=>import('../views/profile/Login')
const Address=()=>import('../views/profile/Address')
const AddressEdit=()=>import('../views/profile/AddressEdit')
const CreateOrder=()=>import('../views/order/CreateOrder')
const Order=()=>import('../views/order/Order')
const OrderDetail=()=>import('../views/order/OrderDetail')
import store from '../store'
const routes = [
  {
    path: '/',
    name: 'DefaultHome',
    component: Home,
    meta:{
      title:'图书世界'
    }
  },
  {
    path: '/home',
    name: 'Home',
    component: Home,
    meta:{
      title:'图书世界'
    }
  },
  {
    path: '/category',
    name: 'Category',
    component: Category,
    meta:{
      title:'图书世界-商品分类'
    }
  },
  {
    path: '/detail',
    name: 'Detail',
    component: Detail,
    meta:{
      title:'图书世界-商品详情'
    }
  },
  {
    path: '/profile',
    name: 'Profile',
    component: Profile,
    meta:{
      title:'图书世界-个人中心',
      isAuthRequired:true
    }
  },
  {
    path: '/shopcart',
    name: 'Shopcart',
    component: Shopcart,
    meta:{
      title:'图书世界-购物车',
      isAuthRequired:true
    }
  },
  {
    path: '/address',
    name: 'Address',
    component: Address,
    meta:{
      title:'图书世界-地址管理',
      isAuthRequired:true
    }
  },
  {
    path: '/addressedit',
    name: 'AddressEdit',
    component: AddressEdit,
    meta:{
      title:'图书世界-地址编辑',
      isAuthRequired:true
    }
  },
  {
    path: '/register',
    name: 'Register',
    component: Register,
    meta:{
      title:'图书世界-注册页面'
    }
  },
  {
    path: '/login',
    name: 'Login',
    component: Login,
    meta:{
      title:'图书世界-登录页面'
    }
  },
  {
    path: '/createorder',
    name: 'CreateOrder',
    component: CreateOrder,
    meta:{
      title:'图书世界-订单预览',
      isAuthRequired:true
    }
  },
  {
    path: '/order',
    name: 'Order',
    component: Order,
    meta:{
      title:'图书世界-订单列表',
      isAuthRequired:true
    }
  },
  {
    path: '/orderdetail',
    name: 'OrderDetail',
    component:OrderDetail,
    meta:{
      title:'图书世界-订单详情',
      isAuthRequired:true
    }
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})
router.beforeEach((to, from,next) => {
  // ...
  // 返回 false 以取消导航
 if(to.meta.isAuthRequired&&store.state.user.isLogin===false){
    Notify('你还没有登录，请先登录')
    return next({path:'/login'})
 }else{
   next()
 }
  document.title=to.meta.title
})
export default router
